﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace SelfManagement.Utility.Extensions
{
    public static class DateTimeExtensions
    {
        #region 日期格式转换

        /// <summary>
        /// Converts to yyyy-mm-dd format date string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static string ToLocalYYYYMMDD(this DateTime date)
        {
            return date/*.ToLocalTime()*/.ToString("yyyy-MM-dd");
        }

         /// <summary>
         /// 返回可读的日期
         /// </summary>
         /// <param name="date"></param>
         /// <returns></returns>
        public static string ToLocalReadableDate(this DateTime date)
        {
            var timeSpan = DateTime.Now - date;
            if (timeSpan.Days < 7)
            {
                //还没超过24小时，并且还是同一天，显示时间
                if ((int)timeSpan.TotalDays == 0 && date.Date == DateTime.Now.Date)
                {
                    return date/*.ToLocalTime()*/.ToString("HH:mm");
                }
                //还没超过24小时，但是超过了一天，显示几小时前
                else if ((int)timeSpan.TotalDays == 0)
                {
                    if (timeSpan.TotalHours > 0)//超过1小时
                    {
                        return $"{(int)timeSpan.TotalHours}小时前";
                    }
                    else//几分钟前
                    {
                        return $"{(int)timeSpan.TotalMinutes}分钟前";
                    }
                }
                else
                {
                    return $"{(int)timeSpan.TotalDays}天前";
                }
            }
            else
            {
                return date.ToLocalYYYYMMDD();
            }
        }


        /// <summary>
        /// Converts to yyyy-mm-dd HH:mm:ss format date string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static string ToLocalYYYYMMDDHHmmss(this DateTime date)
        {
            return date/*.ToLocalTime()*/.ToString("yyyy-MM-dd HH:mm:ss");
        }


        /// <summary>
        /// Converts to yyyy-mm-dd format date string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static string ToLocalYYYYMMDD(this DateTime? date)
        {
            if (date.HasValue)
            {
                return date.Value/*.ToLocalTime()*/.ToString("yyyy-MM-dd");
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Converts to yyyy-mm-dd HH:mm:ss format date string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static string ToLocalYYYYMMDDHHmmss(this DateTime? date)
        {
            if (date.HasValue)
            {
                return date.Value/*.ToLocalTime()*/.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Converts to yyyy-mm format date string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static string ToLocalYYYYMM(this DateTime date)
        {
            return date/*.ToLocalTime()*/.ToString("yyyy-MM");
        }

        /// <summary>
        /// Converts to yyyy-mm-dd format date string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static string ToLocalYYYYMM(this DateTime? date)
        {
            if (date.HasValue)
            {
                return date.Value/*.ToLocalTime()*/.ToString("yyyy-MM");
            }
            else
            {
                return "";
            }
        }

        #endregion

        #region 日期区间

        /// <summary>
        /// Gets the first day of month of date.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static DateTime FirstDayOfMonth(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1, 0, 0, 0, DateTimeKind.Utc);
        }

        /// <summary>
        /// Gets the first day of nexts the month of current date.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static DateTime NextMonth(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1, 0, 0, 0, DateTimeKind.Utc)
                .AddMonths(1);
        }

        /// <summary>
        /// Gets the first day of month of date.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static DateTime LastDayOfMonth(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month), 23, 59, 59, DateTimeKind.Utc);
        }

        /// <summary>
        /// 判断日期是否在某一个区间内
        /// </summary>
        public static bool IncluedIn(this DateTime source, DateTime start, DateTime end)
        {
            return source >= start && source <= end;
        }

        #endregion

        /// <summary>  
        /// DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        public static string GetTimestamp(this DateTime time, bool millisecond = true)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToUniversalTime(new DateTime(1970, 1, 1));
            return (time - startTime).TotalSeconds.ToString() + (millisecond ? "000" : "");
        }
    }

    public static class DateTimeHelper
    {
        /// <summary>  
        /// 时间戳转为DateTime
        /// </summary> 
        public static DateTime GetDateTimeByTimestamp(string timestamp, bool millisecond = true)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToUniversalTime(new DateTime(1970, 1, 1));
            if (millisecond)
            {
                timestamp = timestamp.Substring(0, timestamp.Length - 3);
            }
            return dtStart.AddSeconds(long.Parse(timestamp));
        }

        /// <summary>  
        /// 时间戳转为DateTime
        /// </summary> 
        public static string GetTimestamp(DateTime time, bool millisecond = true)
        {
            return time.GetTimestamp(millisecond);
        }

    }
}
